package app.view.component.tooltips 
{
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.display.GradientType;
	
	public final class BgToolTip extends Sprite
	{
		private var _parts:Vector.<Bitmap>;
		private var _boundaries:int;
		/**
		 * Create a resizable Background
		 * @param	parent Parent of Background
		 * @param	width Initial width
		 * @param	height Initial height
		 * @param	boundaries Boundaries to slice the image
		 * @param	smoothing Smooth the Background
		 * @param	cacheAsBitmap Store the Background in memory
		 */
		public function BgToolTip(parent:DisplayObjectContainer, width:Number = NaN, height:Number = NaN, boundaries:int = 10, smoothing:Boolean = true, cacheAsBitmap:Boolean = false) 
		{
			var source:Sprite =  createBgTemplate();
			var parts:Vector.<Bitmap> = new Vector.<Bitmap> ();
			var m:Matrix = source.transform.matrix;
			
			parts[0] = BitmapUtils.snapShot(source, boundaries, boundaries, null, smoothing);
			
			m.translate( -boundaries, 0);
			parts[1] = BitmapUtils.snapShot(source, source.width - boundaries * 2, boundaries, m, smoothing);
			
			m.identity();
			m.translate( -source.width + boundaries, 0);
			parts[2] = BitmapUtils.snapShot(source, boundaries, boundaries, m, smoothing);
			
			m.identity();
			m.translate( 0, -boundaries);
			parts[3] = BitmapUtils.snapShot(source, boundaries, source.height - boundaries * 2, m, smoothing);
			
			m.identity();
			m.translate( -boundaries, -boundaries);
			parts[4] = BitmapUtils.snapShot(source, source.width - boundaries * 2, source.height - boundaries * 2, m, smoothing);
			
			m.identity();
			m.translate( -source.width + boundaries, -boundaries);
			parts[5] = BitmapUtils.snapShot(source, boundaries, source.height - boundaries * 2, m, smoothing);
			
			m.identity();
			m.translate(0, -source.height + boundaries);
			parts[6] = BitmapUtils.snapShot(source, boundaries, boundaries, m, smoothing);	
			
			m.identity();
			m.translate(-boundaries, -source.height + boundaries);
			parts[7] = BitmapUtils.snapShot(source, source.width - boundaries * 2, boundaries, m, smoothing);	
			
			m.identity();
			m.translate(-source.width + boundaries, -source.height + boundaries);
			parts[8] = BitmapUtils.snapShot(source, boundaries, boundaries, m, smoothing);

			this.addChild(parts[0]);
			this.addChild(parts[1]);
			this.addChild(parts[2]);
			this.addChild(parts[3]);
			this.addChild(parts[4]);
			this.addChild(parts[5]);
			this.addChild(parts[6]);
			this.addChild(parts[7]);
			this.addChild(parts[8]);
			
			this._parts = parts;
			this._boundaries = boundaries;
			
			this.width = (isNaN(width)) ? source.width : width;
			this.height = (isNaN(height)) ? source.height : height;
			
			parent.addChild(this);
		}
		
		private function arrange():void
		{
			var parts:Vector.<Bitmap> = this._parts;
			var boundaries:int = this._boundaries;
			
			parts[0].x = 0;
			parts[0].y = 0;
			
			parts[1].x = boundaries;
			parts[1].y = 0;
			
			parts[2].x = parts[0].width + parts[1].width;
			parts[2].y = 0;
			
			parts[3].x = 0;
			parts[3].y = boundaries;
			
			parts[4].x = boundaries;
			parts[4].y = boundaries;
			
			parts[5].x = parts[3].width + parts[4].width;
			parts[5].y = boundaries;
			
			parts[6].x = 0;
			parts[6].y = parts[0].height + parts[3].height;
			
			parts[7].x = boundaries;
			parts[7].y = parts[6].y;
			
			parts[8].x = parts[6].width + parts[7].width;
			parts[8].y = parts[6].y;
		}
		public override function set width(v:Number):void
		{
			var parts:Vector.<Bitmap> = this._parts;
			var boundaries:int = this._boundaries;
			
			parts[1].width = v - boundaries * 2;
			parts[4].width = v - boundaries * 2;
			parts[7].width = v - boundaries * 2;
			
			this.arrange();
		}
		public override function set height(v:Number):void
		{
			var parts:Vector.<Bitmap> = this._parts;
			var boundaries:int = this._boundaries;
			
			parts[3].height = v - boundaries * 2;
			parts[4].height = v - boundaries * 2;
			parts[5].height = v - boundaries * 2;			
			
			this.arrange();
		}
		
		private static function createBgTemplate():Sprite {
			var _bg:Sprite = new Sprite();
			var b:Number = 1;
			var r:Number = 7;
			var _w:Number = 250;
			var _h:Number = 100;
			var matr:Matrix = new Matrix();
			var lineColor:uint = 0x442702;
			
				/*
				1_ ____________________2_____________________ _3
				|__|________________________________________|__|
				|  |                                        |  |
			   4|  |                  5                     |  |6
				|  |                                        |  |
				|__|________________________________________|__|
				|__|________________________________________|__|9
				7					  8
				*/
				
				
				
				//1. Vẽ nên màu vàng đậm, màu nền này sẽ là màu của border.
				_bg.graphics.beginFill(0xfffc00);
				_bg.graphics.drawRoundRectComplex(0, 0, _w, _h, r, r, r, r);
				
				//2. Vẽ nên gradient vàng bên trong.
				//matr.createGradientBox(_w - 2 * b, _h - 2 * b, Math.PI * 0.5, b, b);
				//_bg.graphics.beginGradientFill(GradientType.LINEAR, [0xF8F3C3, 0x615847, 0xc79a3d], [1, 1, 1], [0x00, 0x80, 0xFF], matr);			
				//_bg.graphics.drawRoundRectComplex(b, b, _w - 2 * b, _h - 2 * b, r, r, r, r);
				
				//3. Vẽ nền vàng nhạt bên trong.
				//_bg.graphics.beginFill(0x7C642C);
				//_bg.graphics.drawRoundRectComplex(2 * b, 2 * b, _w - 4 * b, _h - 4 * b, r, r, r, r);
				//
				//_bg.graphics.beginFill(0xBF9801);
				//_bg.graphics.drawRoundRectComplex(3 * b, 3 * b, _w - 6 * b, _h - 6 * b, r, r, r, r);

				matr.createGradientBox(_w - 2 * b, _h - 2 * b, Math.PI * 0.5, b, b);
				_bg.graphics.beginGradientFill(GradientType.LINEAR, [0xfdb032, 0xf0e256, 0xfdb032], [1, 1, 1], [0x00, 0x80, 0xFF], matr);			
				_bg.graphics.drawRoundRectComplex(4 * b, 4 * b, _w - 8 * b, _h - 8 * b, r, r, r, r);
				
				_bg.graphics.endFill();
				
			return _bg;
		}
		
		private static function createBgTemplate1():Sprite {
			var _bg:Sprite = new Sprite();
			var b:Number = 7 * 2;
			var r:Number = 7;
			var _w:Number = 250;
			var _h:Number = 100;
			var matr:Matrix = new Matrix();
			var lineColor:uint = 0x442702;
			
				/*
				1_ ____________________2_____________________ _3
				|__|________________________________________|__|
				|  |                                        |  |
			   4|  |                  5                     |  |6
				|  |                                        |  |
				|__|________________________________________|__|
				|__|________________________________________|__|9
				7					  8
				*/
				
				matr.createGradientBox(b, b, 0, 0, 0);
				_bg.graphics.beginGradientFill(GradientType.LINEAR, [0xC4952B, 0xA46C24, 0x995F22, 0x915522], [1, 1, 1, 1], [0x00, 0x64, 0x83, 0xFF], matr);
				/*[1]*/_bg.graphics.drawRoundRectComplex(0, 0, b, b, r, 0, 0, 0);
				matr.createGradientBox(b, b, 0, 0, _h-b);
				_bg.graphics.beginGradientFill(GradientType.LINEAR, [0xC4952B, 0xA46C24, 0x995F22, 0x915522], [1, 1, 1, 1], [0x00, 0x64, 0x83, 0xFF], matr);			
				/*[7]*/_bg.graphics.drawRoundRectComplex(0, _h-b, b, b, 0, 0, r, 0);
				matr.createGradientBox(b, b, -Math.PI, _w - b, 0);
				_bg.graphics.beginGradientFill(GradientType.LINEAR, [0x8F5220, 0x945924, 0xA26D2F], [1, 1, 1], [0x00, 0x83, 0xFF], matr);			
				/*[3]*/_bg.graphics.drawRoundRectComplex(_w - b, 0, b, b, 0, r, 0, 0);
				matr.createGradientBox(b, b, -Math.PI, _w - b, _h-b);
				_bg.graphics.beginGradientFill(GradientType.LINEAR, [0x8F5220, 0x945924, 0xA26D2F], [1, 1, 1], [0x00, 0x83, 0xFF], matr);							
				/*[9]*/_bg.graphics.drawRoundRectComplex(_w-b, _h-b, b, b, 0, 0, 0, r);
				matr.createGradientBox(b, _h-b*2, 0, 0, b);
				_bg.graphics.beginGradientFill(GradientType.LINEAR, [0xC4952B, 0xA46C24, 0x995F22, 0x915522], [1, 1, 1, 1], [0x00, 0x64, 0x83, 0xFF], matr);
				/*[4]*/_bg.graphics.drawRect(0, b, b, _h-b*2);
				matr.createGradientBox(b, _h - 2 * b, -Math.PI, _w - b, b);
				_bg.graphics.beginGradientFill(GradientType.LINEAR, [0x8F5220,0x945924,0xA26D2F], [1, 1, 1], [0x00, 0x83, 0xFF], matr);		
				/*[6]*/_bg.graphics.drawRect(_w - b, b, b, _h - 2 * b);
				
				matr.createGradientBox(_w - 2 * b, b, 0, b, 0);
				_bg.graphics.beginGradientFill(GradientType.LINEAR, [0x915522, 0xDFC25E, 0xFEF8C8, 0xA26D2F], [1, 1, 1, 1], [0x00, 0x78, 0xC8, 0xFF], matr);
				/*[2]*/_bg.graphics.drawRect(b, 0, _w - 2 * b, b);			
				/*[5]*/_bg.graphics.drawRect(b, b, _w - 2 * b, _h - 2 * b);
				/*[8]*/_bg.graphics.drawRect(b, _h - b, _w - 2 * b, b);
				_bg.graphics.endFill();
				
				_bg.graphics.lineStyle(1, lineColor);
				/*[4]*/_bg.graphics.moveTo(1, b);
				/*[4]*/_bg.graphics.lineTo(1, _h - b);

				/*[6]*/_bg.graphics.moveTo(_w-2, b);
				/*[6]*/_bg.graphics.lineTo(_w - 2, _h - b);
				
				/*[2]*/_bg.graphics.moveTo(b, 1);
				/*[2]*/_bg.graphics.lineTo(_w - b, 1);
				
				/*[8]*/_bg.graphics.moveTo(b, _h - 2);
				/*[8]*/_bg.graphics.lineTo(_w - b, _h - 2);
				
				var b2:Number = b * 0.5;
				var p:Number = Math.sin(Math.PI * 0.25);
				
				/*[1]*/_bg.graphics.moveTo(b, 1);
				/*[1]*/_bg.graphics.lineTo(b2, 1);
				/*[1]*/_bg.graphics.curveTo(b2 - (b2 - 1) * p, b2 - (b2 - 1) * p, 1, b2);
				/*[1]*/_bg.graphics.lineTo(1, b);
				
				/*[3]*/_bg.graphics.moveTo(_w-2, b);
				/*[3]*/_bg.graphics.lineTo(_w-2, b2);
				/*[3]*/_bg.graphics.curveTo(_w - b2 + (b2 - 1) * p, b2 - (b2 - 1) * p, _w - b2, 1);
				/*[3]*/_bg.graphics.lineTo(_w-b, 1);
				//
				/*[9]*/_bg.graphics.moveTo(_w-b, _h-2);
				/*[9]*/_bg.graphics.lineTo(_w-b2, _h-2);
				/*[9]*/_bg.graphics.curveTo((_w - b2) + (b2 - 1) * p, (_h - b2) + (b2 - 1) * p, _w - 2, _h - b2);
				/*[9]*/_bg.graphics.lineTo(_w - 2, _h - b);
				
				/*[7]*/_bg.graphics.moveTo(1, _h-b);
				/*[7]*/_bg.graphics.lineTo(1, _h-b2);
				/*[7]*/_bg.graphics.curveTo(b2 - (b2 - 1) * p, _h - b2 + (b2 - 1) * p , b2, _h - 2);
				/*[7]*/_bg.graphics.lineTo(b, _h-2);				
				
			return _bg;
		}
		
	}
}